package com.crecheng.shop.Manage;

import com.crecheng.shop.DAO.BrowserData;
import com.crecheng.shop.util.ConSQL;

import java.util.ArrayList;
import java.util.List;

public final class BrowserManage {
    public static List<BrowserData> getAll(){
        return ConSQL.select(BrowserData.class);
    }

    public static List<BrowserData> get(String other){
        return ConSQL.select(BrowserData.class,other);
    }

    public static BrowserData get(int id){
        var d=get("id="+id);
        return d.size()>0?d.get(0):null;
    }

    public static List<BrowserData> getUserLast(int uid,int count){
        var d=getByUser(uid);
        d.sort((a,b)-> -a.getTime().compareTo(b.getTime()));
        if(d.size()<=count)
            return d;
        var list=new ArrayList<BrowserData>();
        for (int i = 0; i < count; i++) {
            list.add(d.get(i));
        }
        return list;
    }

    public static List<BrowserData> getByUser(int uid){
        return get("uid="+uid);
    }

    public static boolean add(BrowserData data){
        return ConSQL.insert(BrowserData.class,data)>0;
    }

    public static boolean del(int id){
        return ConSQL.delete(BrowserData.class,"id="+id)>0;
    }

    public static int delByUser(int uid){
        return ConSQL.delete(BrowserData.class,"uid="+uid);
    }

    public static boolean del(BrowserData data){
        return del(data.getId());
    }
}
